//
//  SleepAnalysisViewModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/4.
//

import UIKit

import RxCocoa
import RxSwift
import MYModule
import SwiftDate

open class SleepAnalysisViewModel: ViewModel {
    
    public let providerRelay: BehaviorRelay<SleepProvider?>
    
    public let efficiency = BehaviorRelay<SleepAnalysisModel?>(value: nil)
    public let latency = BehaviorRelay<SleepAnalysisModel?>(value: nil)
    public let timing = BehaviorRelay<SleepAnalysisModel?>(value: nil)
    

    public init(providerRelay: BehaviorRelay<SleepProvider?>) {
        
        self.providerRelay = providerRelay
        
        super.init()
        
        providerRelay.map {[weak self] in
            
            self?.efficiencyModel(provider: $0)
            
        }.bind(to: efficiency).disposed(by: rx.disposeBag)
        
        providerRelay.map {[weak self] in
            
            self?.latencyModel(provider: $0)
            
        }.bind(to: latency).disposed(by: rx.disposeBag)
        
        providerRelay.map {[weak self] in
            
            self?.timiModel(provider: $0)
            
        }.bind(to: timing).disposed(by: rx.disposeBag)
        
        
    }
    
    
    open func efficiencyModel(provider: SleepProvider?) ->SleepAnalysisModel {
        
        guard let provider = provider else {
            return SleepAnalysisModel.efficiency(number: 0)
        }
        
        let detail = provider.curDayDetail.detail
        var total: CGFloat = 0
        var sleeping: CGFloat = 0
        detail.forEach { d in
            if d.type != 0 {
                sleeping = sleeping + CGFloat(d.total)
            }
            total = total + CGFloat(d.total)
        }
        
        var number = 0
        if sleeping > 0 {
           number = Int(sleeping/total * 100)
        }
        return SleepAnalysisModel.efficiency(number: number)
        
    }
    
    open func latencyModel(provider: SleepProvider?) ->SleepAnalysisModel {
        
        guard let provider = provider else {
            return SleepAnalysisModel.latency(number: 0)
        }
 
        let detail = provider.curDayDetail.detail.first
        return SleepAnalysisModel.latency(number: detail?.total ?? 0)
    }
    
    open func timiModel(provider: SleepProvider?) ->SleepAnalysisModel {
        
        
        guard let provider = provider else {
            return SleepAnalysisModel.timing(type: nil)
        }
        
        // 获取所有睡眠时间段
        let sleepingDetail = provider.curDayDetail.detail.filter { d in d.type > 0  }
        
        var type = "--".language
        if sleepingDetail.isEmpty {
            return SleepAnalysisModel.timing(type: type)
        }
        
        // 获取中点
        let centerDetail = sleepingDetail[Int(sleepingDetail.count/2)]
        
        guard let date = centerDetail.longStartDate(with: provider.date) else {
            return SleepAnalysisModel.timing(type: type)
        }
        
        if centerDetail.isToday {
            
            // 3 - 4 点
            if let today_3 = provider.date.dateBySet(hour: 3, min: 0, secs: nil),
               let today_4 = provider.date.dateBySet(hour: 4, min: 0, secs: nil) {
                
                if date.isAfterDate(today_3, orEqual: true, granularity: .minute) && date.isBeforeDate(today_4, orEqual: true, granularity: .minute)  {
                    
                    type = "sleep_good".language
                }
                
            }

        } else {
            
            // 11 - 0 点
            let yesterday = provider.date - 1.days
            if let yesterDay_23 = yesterday.dateBySet(hour: 23, min: 0, secs: nil)?.date {
                
                if date.isAfterDate(yesterDay_23, orEqual: true, granularity: .minute) {
                    
                    type = "sleep_optimal".language
                }
            }
        }
        
        if type == "--" {
            type = "sleep_poor".language
        }
        
        return SleepAnalysisModel.timing(type: type)
        
    }
    
    
    
    

}

